package com.example.light.web.personal.service.impl;

import com.example.light.common.exception.GlobalException;
import com.example.light.common.result.CodeMsg;
import com.example.light.web.personal.dao.SchedualMapper;
import com.example.light.web.personal.entity.Schedual;
import com.example.light.web.personal.service.ScheduleService;
import com.example.light.web.personal.vo.ScheduleVO;
import com.example.light.web.system.entity.User;
import com.fasterxml.jackson.databind.util.ISO8601DateFormat;
import com.fasterxml.jackson.databind.util.ISO8601Utils;
import com.google.common.collect.Lists;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ScheduleServiceImpl implements ScheduleService {
    @Autowired
    private SchedualMapper schedualMapper;

    @Override
    public List<ScheduleVO> showSchedules(User user) {
        List<Schedual> scheduals = schedualMapper.selectUserSchedules(user.getUserId());
        List<ScheduleVO> res = Lists.newArrayList();
        for (Schedual schedual : scheduals) {
            ScheduleVO scheduleVO = new ScheduleVO();
            BeanUtils.copyProperties(schedual, scheduleVO);
            DateTime start = new DateTime(schedual.getSchedualStart());
            DateTime end = new DateTime(schedual.getSchedualEnd());
            scheduleVO.setSchedualStart(start.toString());
            scheduleVO.setSchedualEnd(end.toString());
            Integer[] showTime = new Integer[4];
            showTime[0] = start.getDayOfMonth();
            showTime[1] = start.getHourOfDay();
            showTime[2] = end.getDayOfMonth();
            showTime[3] = end.getHourOfDay();
            scheduleVO.setShowTime(showTime);
            res.add(scheduleVO);
        }
        return res;
    }

    @Override
    public boolean addSchedule(ScheduleVO schedule, Integer userId) {
        Schedual schedual = new Schedual();
        schedual.setUserId(userId);
        schedual.setSchedualContent(schedule.getSchedualContent());
        schedual.setSchedualTitle(schedule.getSchedualTitle());
        schedual.setSchedualStatus((byte) -1);
        DateTime start = DateTime.parse(schedule.getSchedualStart(), DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"));
        DateTime end = DateTime.parse(schedule.getSchedualEnd(), DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"));
        schedual.setSchedualStart(start.toDate());
        schedual.setSchedualEnd(end.toDate());

        int succ = schedualMapper.insertSelective(schedual);
        if (succ > 0) {
            return true;
        }
        throw new GlobalException(CodeMsg.PersonalCodeMsg.ADD_SCHEDULE_FAIL);
    }

    @Override
    public void delSchedule(Integer id) {
        schedualMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void finish(Integer id) {
        schedualMapper.finish(id);
    }

    @Override
    public ScheduleVO showOneSchedule(Integer scheduleId) {
        Schedual schedual = schedualMapper.selectByPrimaryKey(scheduleId);
        ScheduleVO scheduleVO = new ScheduleVO();
        BeanUtils.copyProperties(schedual,scheduleVO);
        Date start = schedual.getSchedualStart();
        Date end = schedual.getSchedualEnd();
//        String showStart = ISO8601Utils.format(start);
//        String showEnd = ISO8601Utils.format(end);
        DateTime startTime = new DateTime(start);
        DateTime endTime = new DateTime(end);
        scheduleVO.setSchedualStart(startTime.toString());
        scheduleVO.setSchedualEnd(endTime.toString());
        scheduleVO.setSchedualId(scheduleId);
        return scheduleVO;
    }

    @Override
    public boolean modifySchedule(ScheduleVO schedule, Integer userId) {
        Schedual schedual = new Schedual();
        BeanUtils.copyProperties(schedule,schedual);
        DateTime start = DateTime.parse(schedule.getSchedualStart(), DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"));
        DateTime end = DateTime.parse(schedule.getSchedualEnd(), DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"));
        schedual.setSchedualStart(start.toDate());
        schedual.setSchedualEnd(end.toDate());
        int succ = schedualMapper.updateByPrimaryKeySelective(schedual);
        if(succ <= 0){
            throw new GlobalException(CodeMsg.SystemCodeMsg.ERROR);
        }
        return true;
    }
}
